<template>
	<view class="circle-info-card">
		<!-- 圈子头部信息 -->
		<view class="circle-header">
			<view class="circle-avatar">
				<image :src="circleInfo?.circleAvatar" mode="aspectFill"
					class="avatar-image" />
			</view>
			<view class="circle-basic">
				<text class="circle-name">{{ circleInfo?.circleName || '未知圈子' }}</text>
				<view class="circle-owner">
					<text class="owner-label">圈主:</text>
					<text class="owner-name">{{ circleInfo?.ownerUser || '未知' }}</text>
				</view>
			</view>
		</view>

		<!-- 圈子描述 -->
		<view class="circle-description" v-if="circleInfo?.circleDescription">
			<text class="description-text">{{ circleInfo.circleDescription }}</text>
		</view>

		<!-- 圈子统计信息 -->
		<view class="circle-stats">
			<view class="stat-item">
				<text class="stat-number">{{ circleInfo?.memberCount || 0 }}</text>
				<text class="stat-label">成员</text>
			</view>
			<view class="stat-divider"></view>
			<view class="stat-item">
				<text class="stat-number">{{ circleInfo?.postCount || 0 }}</text>
				<text class="stat-label">帖子</text>
			</view>
			<view class="stat-divider"></view>
			<view class="stat-item">
				<text class="stat-number">{{ circleInfo?.heatScore || 0 }}</text>
				<text class="stat-label">热度</text>
			</view>
		</view>

		<!-- 圈子状态 -->
		<view class="circle-status" v-if="circleInfo?.approvalStatus">
			<view class="status-badge" :class="getStatusClass(circleInfo.approvalStatus)">
				<text class="status-text">{{ getStatusText(circleInfo.approvalStatus) }}</text>
			</view>
		</view>

		<!-- 操作按钮 -->
		<view class="circle-actions">
			<button class="action-btn" :class="isJoined ? 'joined' : 'primary'"
				@click="isJoined ? handleLeaveCircle() : handleJoinCircle()">
				<uni-icons :type="isJoined ? 'checkmarkempty' : 'plus'" size="16"
					:color="isJoined ? '#52c41a' : 'white'"></uni-icons>
				<text>{{ isJoined ? '已加入' : '加入圈子' }}</text>
			</button>
			<button class="action-btn secondary" @click="handleShareCircle">
				<uni-icons type="redo" size="16" color="#007AFF"></uni-icons>
				<text>分享</text>
			</button>
		</view>
	</view>
</template>

<script setup lang="ts">
	import { defineProps, defineEmits, computed } from 'vue'
	import type { CircleBasicInfo } from '@/api/circle/circle'

	// Props定义
	interface Props {
		circleInfo ?: CircleBasicInfo
		joinedCircles ?: any[] // 已加入的圈子列表
	}

	const props = withDefaults(defineProps<Props>(), {
		circleInfo: () => ({} as CircleBasicInfo),
		joinedCircles: () => []
	})

	// 事件定义
	const emit = defineEmits<{
		joinCircle : [circleInfo: CircleBasicInfo]
		shareCircle : [circleInfo: CircleBasicInfo],
		leaveCircle : [circleInfo: CircleBasicInfo]
	}>()

	// 计算是否已加入当前圈子
	const isJoined = computed(() => {
		if (!props.circleInfo?.id || !props.joinedCircles?.length) {
			return false
		}
		return props.joinedCircles.some(circle => circle.id === props.circleInfo.id)
	})

	// 获取状态样式类
	const getStatusClass = (status : string) => {
		switch (status) {
			case '1':
			case 'approved':
				return 'status-approved'
			case '0':
			case 'pending':
				return 'status-pending'
			case '-1':
			case 'rejected':
				return 'status-rejected'
			default:
				return 'status-unknown'
		}
	}

	// 获取状态文本
	const getStatusText = (status : string) => {
		switch (status) {
			case '1':
			case 'approved':
				return '已审核'
			case '0':
			case 'pending':
				return '审核中'
			case '-1':
			case 'rejected':
				return '已拒绝'
			default:
				return '未知状态'
		}
	}

	// 处理加入圈子
	const handleJoinCircle = () => {
		if (props.circleInfo && !isJoined.value) {
			emit('joinCircle', props.circleInfo)
		}
	}
	
	// 处理退出圈子
	const handleLeaveCircle = () => {
		if (props.circleInfo && isJoined.value) {
			emit('leaveCircle', props.circleInfo)
		}
	}
	
	// 处理分享圈子
	const handleShareCircle = () => {
		if (props.circleInfo) {
			emit('shareCircle', props.circleInfo)
		}
	}
</script>

<style lang="scss" scoped>
	.circle-info-card {
		background: white;
		margin: 16rpx 24rpx;
		padding: 32rpx;
		border-radius: 16rpx;
		box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.08);
	}

	.circle-header {
		display: flex;
		gap: 24rpx;
		margin-bottom: 24rpx;

		.circle-avatar {
			width: 120rpx;
			height: 120rpx;
			border-radius: 16rpx;
			overflow: hidden;
			border: 2rpx solid #f0f0f0;

			.avatar-image {
				width: 100%;
				height: 100%;
			}
		}

		.circle-basic {
			flex: 1;
			display: flex;
			flex-direction: column;
			justify-content: center;
			gap: 8rpx;

			.circle-name {
				font-size: 36rpx;
				font-weight: 600;
				color: #333;
				line-height: 1.3;
			}

			.circle-owner {
				display: flex;
				align-items: center;
				gap: 8rpx;

				.owner-label {
					font-size: 24rpx;
					color: #666;
				}

				.owner-name {
					font-size: 24rpx;
					color: #007AFF;
					font-weight: 500;
				}
			}
		}
	}

	.circle-description {
		margin-bottom: 24rpx;
		padding: 20rpx;
		background: #f8f9fa;
		border-radius: 12rpx;

		.description-text {
			font-size: 28rpx;
			color: #666;
			line-height: 1.5;
		}
	}

	.circle-stats {
		display: flex;
		align-items: center;
		justify-content: space-around;
		margin-bottom: 24rpx;
		padding: 20rpx 0;

		.stat-item {
			display: flex;
			flex-direction: column;
			align-items: center;
			gap: 8rpx;

			.stat-number {
				font-size: 32rpx;
				font-weight: 600;
				color: #333;
			}

			.stat-label {
				font-size: 24rpx;
				color: #999;
			}
		}

		.stat-divider {
			width: 1rpx;
			height: 40rpx;
			background: #eee;
		}
	}

	.circle-status {
		margin-bottom: 24rpx;
		display: flex;
		justify-content: center;

		.status-badge {
			padding: 8rpx 16rpx;
			border-radius: 20rpx;

			.status-text {
				font-size: 24rpx;
				font-weight: 500;
			}

			&.status-approved {
				background: #f0f9ff;

				.status-text {
					color: #0ea5e9;
				}
			}

			&.status-pending {
				background: #fef3c7;

				.status-text {
					color: #d97706;
				}
			}

			&.status-rejected {
				background: #fee2e2;

				.status-text {
					color: #dc2626;
				}
			}

			&.status-unknown {
				background: #f3f4f6;

				.status-text {
					color: #6b7280;
				}
			}
		}
	}

	.circle-actions {
		display: flex;
		gap: 16rpx;

		.action-btn {
			flex: 1;
			display: flex;
			align-items: center;
			justify-content: center;
			gap: 8rpx;
			height: 80rpx;
			border-radius: 12rpx;
			font-size: 28rpx;
			font-weight: 500;
			border: none;

			&.primary {
				background: #007AFF;
				color: white;
			}

			&.joined {
				background: #f6ffed;
				color: #52c41a;
				border: 2rpx solid #b7eb8f;
				cursor: not-allowed;
			}

			&.secondary {
				background: white;
				color: #007AFF;
				border: 2rpx solid #007AFF;
			}

			&:active:not(:disabled) {
				transform: scale(0.98);
				transition: transform 0.1s ease;
			}

			&:disabled {
				opacity: 0.8;
			}
		}
	}
</style>